package kr.co.doriclassic.summary_java.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import kr.co.doriclassic.mylibrary.DbUtil;
import kr.co.doriclassic.summary_java.dto.Department;
import kr.co.doriclassic.summary_java.dto.Employee;

import org.apache.log4j.Logger;


public class DepartmentDao implements InterfaceDao<Department> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(DepartmentDao.class);

	private Connection connection;
	private PreparedStatement pstmt;
	
	public DepartmentDao(Connection connection) {
		this.connection = connection;
	}

	
	@Override
	public void insertItem(Department item) throws SQLException {
		if (logger.isDebugEnabled()) {
			logger.debug("insertItem(Department) - start");
		}

		String sql = "insert into department values(?,?,?)";
		pstmt = connection.prepareStatement(sql);
		pstmt.setInt(1, item.getDeptNo());
		pstmt.setString(2, item.getDeptName());
		pstmt.setInt(3, item.getFloor());
		pstmt.executeUpdate();
		DbUtil.close(pstmt);	

		if (logger.isDebugEnabled()) {
			logger.debug("insertItem(Department) - end");
		}
	}

	@Override
	public void deleteItembyNo(Department item) throws SQLException {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteItembyNo(Department) - start");
		}

		String sql = "delete from department where deptno = ?";
		pstmt = connection.prepareStatement(sql);
		pstmt.setInt(1, item.getDeptNo());
		pstmt.executeUpdate();
		DbUtil.close(pstmt);

		if (logger.isDebugEnabled()) {
			logger.debug("deleteItembyNo(Department) - end");
		}
	}

	@Override
	public void updateItembyNo(Department item) throws SQLException {
		if (logger.isDebugEnabled()) {
			logger.debug("updateItembyNo(Department) - start");
		}

		String sql = "update department set deptname=?, floor=? where deptno=?";
		pstmt = connection.prepareStatement(sql);
		pstmt.setString(1, item.getDeptName());
		pstmt.setInt(2, item.getFloor());
		pstmt.setInt(3, item.getDeptNo());
		pstmt.executeUpdate();
		DbUtil.close(pstmt);		

		if (logger.isDebugEnabled()) {
			logger.debug("updateItembyNo(Department) - end");
		}
	}

	@Override
	public ArrayList<Department> listItems() throws SQLException {
		if (logger.isDebugEnabled()) {
			logger.debug("listItems() - start");
		}

		ArrayList<Department> list = new ArrayList<>();
		String sql = "select deptno, deptname, floor from department";
		pstmt = connection.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();

		while(rs.next()){
			list.add(new Department(
					rs.getInt("deptno"), 
					rs.getString("deptname"), 
					rs.getInt("floor")));
		}
		DbUtil.close(rs);
		DbUtil.close(pstmt);

		if (logger.isDebugEnabled()) {
			logger.debug("listItems() - end");
		}
		return list;
	}

	@Override
	public Department searchItem(Department item) throws SQLException {
		if (logger.isDebugEnabled()) {
			logger.debug("searchItem(Department) - start");
		}

		Department department = null;
		String sql = "select deptno, deptname, floor from department where deptno=?";
		pstmt = connection.prepareStatement(sql);
		pstmt.setInt(1, item.getDeptNo());
		ResultSet rs = pstmt.executeQuery();
		
		if(rs.next()){
			department = new Department();
			department.setDeptNo(rs.getInt(1));
			department.setDeptName(rs.getString(2));
			department.setFloor(rs.getInt(3));
		}

		if (logger.isDebugEnabled()) {
			logger.debug("searchItem(Department) - end");
		}
		return department;
	}

	@Override
	public ArrayList<Department> searchList(String item) throws SQLException {
		if (logger.isDebugEnabled()) {
			logger.debug("searchItem(Department) - start");
		}
		
		ArrayList<Department> list = new ArrayList<>();
		String sql = "select deptno, deptname, floor "
				+ "from department "
				+ "where deptno regexp ? or deptname regexp ? or floor regexp ?";
		pstmt = connection.prepareStatement(sql);
		pstmt.setString(1, item);
		pstmt.setString(2, item);
		pstmt.setString(3, item);
		ResultSet rs = pstmt.executeQuery();
		
		if(rs.next()){
			list.add(new Department(
				rs.getInt("deptno"),	
				rs.getString("deptname"),	
				rs.getInt("floor")));
		}
		DbUtil.close(rs);
		DbUtil.close(pstmt);
		
		
		if (logger.isDebugEnabled()) {
			logger.debug("searchItem(Department) - end");
		}
		return list;
	}
	@Override
	public void prnItemList(Comparator<Department> comparator) throws SQLException {
		if (logger.isDebugEnabled()) {
			logger.debug("prnItemList(Comparator) - start");
		}

		ArrayList<Department> list=listItems();
		if(comparator == null){
			Collections.sort(list);			
		}
		else{
			Collections.sort(list, comparator);
		}
		for(Department emp : list){
			System.out.println(emp);
		}		

		if (logger.isDebugEnabled()) {
			logger.debug("prnItemList(Comparator) - end");
		}
	}
	
	
}
